Rapid-Q Documentation by William Yu (c)1999 Appendix C: Loops


Introduction to Looping

Loops provide a mechanism for executing a series of instructions a certain number of times. Many of your programs will involve looping. Compilers convert loops to GOTO statements, so you could implement your own LOOP using GOTOs without any consequences. However, for optimization purposes, the FOR loop in Rapid-Q doesn't use this mechanism to save on unnecessary instructions. You may notice that your FOR loop is slightly faster than your WHILE or DO loop in Rapid-Q.

FOR .. NEXT Loop
As previously stated, in Rapid-Q this looping structure is optimized, but you may only notice a slight difference. There are some special cases when using a FOR loop in Rapid-Q.
    FOR I(1) = 1 to 100
    NEXT
This is not valid in Rapid-Q. Why not? Because I(1) is an array, you'll get a compiler error. Only simple types are supported, which means anything BUT arrays. The default increment value is 1, if you want a different increment value, please use STEP:
    FOR I = 1 TO 100 STEP 2
      PRINT I
    NEXT
This will skip all the even numbers, so the output will be: 1 3 5 7 9 ...etc. If for whatever reason you wanted to exit the loop, you can use EXIT FOR.

WHILE .. WEND
If you want to test a condition before entering the loop, you could use a WHILE .. WEND loop. There are no special cases when using this loop, it's exactly like QBasic:
    WHILE I < 100
      I++          '-- Increment I
    WEND
It's much like a DO .. LOOP, except the expression is tested before entering the loop. To exit the WHILE .. WEND loop for whatever reason, you use EXIT WHILE.

DO .. LOOP
A simple DO .. LOOP without any test expression will loop infinitely:
    DO
      PRINT " Hello World! "
    LOOP
To exit the DO .. LOOP, you use EXIT DO. Infinite loops aren't used much in Windows programming (event based anyway). You'll usually want a test expression as in our WHILE .. WEND loop, except we place it differently:
    DO
      I++
    LOOP UNTIL I >= 100
Unlike QBasic or other implementations of BASIC, Rapid-Q does not offer DO WHILE or LOOP WHILE. They're basically useless (ie. they can be implemented in another way), except to provide users with more options. For example, this is valid in QBasic:
   DO
   LOOP WHILE I < 100
Which is equivalent to:
   DO
   LOOP UNTIL I >= 100



<- Conditions Contents Methods ->